package com.example.carconsumer.controller.rsh;

import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.POIXMLException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Api(value = "解析保险合同文档格式模块",tags = "解析保险合同文档API")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserCommitOrderController {

    @RequestMapping(value = "/redUserOrder", method = RequestMethod.POST)
    @ApiOperation(value = "用于用户端User保单的word识别接口", notes = "识别上传的用户端user文件", httpMethod = "post", response = ResultJson.class)
    public ResultJson redWordFile(@ApiParam(name = "doc", value = "用户端保单word文档", required = true) MultipartFile file, HttpServletRequest request) {
        ResultJson<Object> resultJson = new ResultJson<>();
        InputStream inputStream = null;
        Map<String, Object> map = new HashMap<>();
        try {
            inputStream = file.getInputStream();//将获取的文件存入其中
            String fileName = file.getOriginalFilename();//获取文件名
            XWPFDocument doc = null; //用于存储解析后的Excel文件
            //判断文件扩展名为“.xls还是xlsx的Excel文件”,因为不同扩展名的Excel所用到的解析方法不同
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            if (".docx".equals(fileType) || ".doc".equals(fileType)) {
                Workbook workbook = null;
                Iterator<XWPFTable> itTable = null;
                try {
                    doc = new XWPFDocument(inputStream);//HSSFWorkbook专门解析.xls文件
                    itTable = doc.getTablesIterator();
                } catch (POIXMLException e) {
                    e.printStackTrace();
                    resultJson.setFail("失败，文件格式错误");
                    return resultJson;
                }
                int ind = 0;//记录表格数
                while (itTable.hasNext()) {
                    ind++;
                    XWPFTable table = (XWPFTable) itTable.next();
                    //行
                    int rcount = table.getNumberOfRows();
                    for (int i = 0; i < rcount; i++) {
                        XWPFTableRow row = table.getRow(i);
                        //列
                        List<XWPFTableCell> cells = row.getTableCells();
                        int len = cells.size();
                        for (int j = 0; j < len; j++) {
                            XWPFTableCell xc = cells.get(j + 1);
                            String sc = xc.getText();
                            System.out.println("第" + ind + "个表格，第" + (i + 1) + "行，第" + (j + 1) + "列：" + sc);
                            if (i == 0 && j == 1) {
                                map.put("id", sc);
                            }
                            if (i == 0 && j == 3) {
                                map.put("name", sc);
                            }
                            if (i == 0 && j == 5) {
                                map.put("origin", sc);
                            }
                            if (i == 1 && j == 1) {
                                map.put("idCard", sc);
                            }
                            if (i == 1 && j == 3) {
                                map.put("tel", sc);
                            }
                            if (i == 1 && j == 5) {
                                map.put("licensePlateNumber", sc);
                            }
                            if (i == 2 && j == 1) {
                                map.put("insurancePolicyNo", sc);
                            }
                            if (i == 2 && j == 3) {
                                map.put("businessInsurancePolicyNo", sc);
                            }
                            if (i == 3 && j == 1) {
                                map.put("createTime", sc);
                            }
                            if (i == 3 && j == 3) {
                                map.put("deadTime", sc);
                            }
                            if (i == 4 && j == 1) {
                                map.put("createTime", sc);
                            }
                            if (i == 4 && j == 3) {
                                map.put("deadTime", sc);
                            }

                            //haiyou
                        }
                    }
                }
                resultJson.setSuccess("成功", map);
                return resultJson;
            } else {
                resultJson.setFail("失败，文件格式错误");
                return resultJson;
            }
        } catch (IOException e) {
            e.printStackTrace();
            resultJson.setFail("未知错误,失败");
            return resultJson;
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}